﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using FireFly;
using System.Collections;

namespace XAMLStylePad.ViewModels
{
    public class DependencyObjectViewModel : XamlViewModelBase<DependencyObject>
    {
        public DependencyObjectViewModel(DependencyObject obj): base(obj)
        {
        }

        #region ObjectChildren
        public CollectionViewModel<DependencyObject, DependencyObjectViewModel> ObjectChildren
        {
            get
            {
                if(_Children == null)
                {
                    int count = VisualTreeHelper.GetChildrenCount(Model);
                    var _subItems = new List<DependencyObject>(count);
                    for(int i = 0; i < count; i++)
                    {
                        _subItems.Add(VisualTreeHelper.GetChild(Model, i));
                    }
                    
                    _Children = new CollectionViewModel<DependencyObject, DependencyObjectViewModel>
                        (_subItems, obj => new DependencyObjectViewModel(obj));
                }
                return _Children;
            }
        }
        CollectionViewModel<DependencyObject, DependencyObjectViewModel> _Children;
        #endregion

        public override ImageSource Icon
        {
            get
            {
                return XamlSPViewModelService.GetToolboxIcon(Model);
            }
        }

        public override string DisplayName
        {
            get
            {
                if(Model is FrameworkElement)
                    return string.Format("{0}({1})", (Model as FrameworkElement).Name, Model.GetType().Name);
                else
                    return Model.GetType().Name;
            }
        }

        public override ICollection VisualChildren
        {
            get
            {
                return ObjectChildren;
            }
        }

        internal bool IsSameModel(DependencyObject newComponent)
        {
            return newComponent == Model;
        }
    }
}
